package com.alex.springbootdemo.controller;

import com.alex.springbootdemo.common.CommonResult;
import com.alex.springbootdemo.config.SecurityConfiguration;
import com.alex.springbootdemo.model.param.UserLoginParam;
import com.alex.springbootdemo.model.pojo.UmsAdmin;
import com.alex.springbootdemo.model.pojo.UmsPermission;
import com.alex.springbootdemo.model.vo.JWTToken;
import com.alex.springbootdemo.security.CurrentUser;
import com.alex.springbootdemo.service.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/admin")
@Api(tags = "UmsAdminController", description = "后台用户管理")
@AllArgsConstructor
public class UmsAdminController {


    private final UmsAdminService umsAdminService;

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public CommonResult<UmsAdmin> register(@RequestBody UmsAdmin param) {
        UmsAdmin umsAdmin = umsAdminService.register(param);
        return CommonResult.success(umsAdmin);
    }


    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public CommonResult login(@Valid @RequestBody UserLoginParam param) {
        JWTToken token  = umsAdminService.login(param);
        return CommonResult.success(token);
    }

    @PostMapping("/refreshToken")
    @ApiOperation(value = "刷新token")
    public CommonResult refreshToken(@Valid @RequestBody RefreshTokenRequest param) {
        JWTToken token  = umsAdminService.refreshToken(param);
        return CommonResult.success(token);
    }

    @Data
    public static class RefreshTokenRequest{
        @NotNull(message = "refreshToken不可为空")
        private String refreshToken;
    }


    @GetMapping("/permission/{adminId}")
    @ApiOperation(value = "获取用户所用权限，包含+-权限")
    public CommonResult login(@PathVariable Long adminId) {
        List<UmsPermission> permissionList = umsAdminService.getPermissionList(adminId);
        return CommonResult.success(permissionList);
    }



}
